//
//  YMCompressionVC.swift
//  swift_review
//
//  Created by Andrew on 2024/12/13.
//

import UIKit
import Compression

class YMCompressionVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        view.addSubview(compreBtn)
        view.addSubview(decompreBtn)
        setupUI()
    }
    
    private func example() {
        // 示例使用
        let uploadParams = UploadParams(
            addID: "123456",
            shan: UUID().uuidString,
            fileName: "example.zip",
            fileSha256: "...", // 从文件计算得到
            secretKey: "...", // Base64 编码的密钥
            fragmentCount: 10,
            fragmentSize: 1024 * 1024, // 每个分片 1 MB
            fileInfo: UploadParams.FileInfo(
                fileSize: 10485760, // 示例文件大小：10 MB
                fileMD5: "..." // 从文件计算得到
            )
        )

        let uploader = YMUploadZip(uploadURL: URL(string: "https://example.com/upload")!)
        uploader.uploadFile(filePath: "/path/to/example.zip", params: uploadParams, token: "your_token_here") { result in
            switch result {
            case .success(let message):
                print("上传完成: \(message)")
            case .failure(let error):
                print("上传失败: \(error.localizedDescription)")
            }
        }
    }
    
    private func setupUI() {
        NSLayoutConstraint.activate([
            compreBtn.topAnchor.constraint(equalTo: view.topAnchor, constant: 100),
            compreBtn.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 50),
            compreBtn.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -50),
            compreBtn.heightAnchor.constraint(equalToConstant: 50),
            
            decompreBtn.topAnchor.constraint(equalTo: compreBtn.bottomAnchor, constant: 40),
            decompreBtn.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 50),
            decompreBtn.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -50),
            decompreBtn.heightAnchor.constraint(equalToConstant: 50),
        ])
    }
    
    private lazy var compreBtn: UIButton = {
        let view = UIButton(type: .custom)
        view.translatesAutoresizingMaskIntoConstraints = false
        view.setTitle("压缩", for: .normal)
        view.backgroundColor = .blue
        view.setTitleColor(.white, for: .normal)
        view.addTarget(self, action: #selector(compreSender), for: .touchUpInside)
        return view
    }()
    
    private lazy var decompreBtn: UIButton = {
        let view = UIButton(type: .custom)
        view.translatesAutoresizingMaskIntoConstraints = false
        view.setTitle("解压", for: .normal)
        view.backgroundColor = .blue
        view.setTitleColor(.white, for: .normal)
        view.addTarget(self, action: #selector(decompreSender), for: .touchUpInside)
        return view
    }()
}

@objc extension YMCompressionVC {
    private func compreSender() {
        example()
    }
    
    private func decompreSender() {
        example()
    }
}
